<?php
declare (strict_types = 1);

namespace app\controller\backend;

use app\model\AdminMenu;
use app\model\Role;
use app\validate\RoleValidate;
use think\exception\ValidateException;


class RoleController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function index(Role $role): \think\response\Json
    {
        $where = [
            'seller_id' => $this->admin['seller_id'],
        ];

        $roleList = $role->getRoleList($where);
        return json($roleList);
    }

    /**
     * 保存新建的资源
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     * @throws \app\exception\ModelNotUniqueException
     */
    public function save(Role $role): \think\response\Json
    {
        if(request()->isPost()){
            $param = request()->only(['title','status']);
            // 数据验证
            try{
                validate(RoleValidate::class)->scene('save')->check($param);
            }catch(ValidateException $e){
                return jsonReturn(-1, $e->getError());
            }
            $param['seller_id'] = $this->admin['seller_id'];

            $role->saveUnique(['seller_id'=>$param['seller_id'],'title'=>$param['title']],'角色已经存在');

            $res = $role -> addRole($param);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }

    /**
     * 显示指定的资源
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     * @throws \app\exception\ModelEmptyException
     */
    public function read(Role $role): \think\response\Json
    {

        $id = (int)input('id');
        if(!$id){
            return jsonReturn(-1,'角色ID不能为空');
        }
        $where = [
            'id' => $id,
            'seller_id' => $this->admin['seller_id']
        ];
        $res = $role->getRole($where);
        $res['data']['auth'] = explode(',',$res['data']['auth']);
        return json($res);

    }

    /**
     * 保存更新的资源
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     * @throws \app\exception\ModelNotUniqueException
     */
    public function update(Role $role): \think\response\Json
    {
        if(request()->isPost()){
            $param = input('post.');

            try {
                validate(RoleValidate::class)->scene('update')->check($param);
            } catch (ValidateException $e) {
                return jsonReturn(-1, $e->getError());
            }
            if($param['group'] == 1){
                jsonReturn(-2,'系统默认角色,不能编辑');
            }
            $role->updateUnique(['seller_id'=>$this->admin['seller_id'],'title'=>$param['title']],$param['id'],'角色已经存在');
            if(!empty($param['auth'])){
                $len   = count($param['auth']);
                foreach($param['auth'] as $key => $val){
                   if($val == 2){
                       break;
                   }
                   if($len == $key+1){
                       array_push($param['auth'],2);
                   }
                }
                $param['auth'] = implode(',',$param['auth']);
            }
            $where = [
                'id' => $param['id'],
                'seller_id' => $this->admin['seller_id'],
            ];
            $res = $role -> updateRole($where,$param);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }

    /**
     * 删除指定资源
     *
     * @throws \app\exception\ModelException
     */
    public function delete(Role $role): \think\response\Json
    {
        if(request()->isPost()){
            $id = (int)input('id');
            if(!$id){
                return jsonReturn(-1,'Id不能为空');
            }
            if($id == 1){
                return jsonReturn(-2,'系统默认角色，不能删除');
            }
            $where = [
                'id' => $id,
                'seller_id' => $this->admin['seller_id']
            ];
            $res = $role->delRole($where);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }


    /**
     * @throws \app\exception\ModelException
     */
    public function getAuth(AdminMenu $AdminMenu): \think\response\Json
    {
        $auth =  $AdminMenu->getAllAdminMenu(['seller_id'=>$this->admin['seller_id']])['data']->toArray();
        $auth = generate($auth);
        return jsonReturn(0,'success',$auth);
    }
}
